package com.atguigu.system.controller;

import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.vo.AssignMenuVo;
import com.atguigu.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("/findNodes")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu permission) {
        sysMenuService.save(permission);
        return Result.ok();
    }


    @ApiOperation("根据id查询菜单")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        //调用SysMenuService中根据id查询的方法
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysMenu permission) {
        permission.setUpdateTime(null);
        sysMenuService.updateById(permission);
        return Result.ok();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysMenuService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("给角色分配权限,回显树")
    @GetMapping("/getRoleMenuList/{roleId}")
    public Result getRoleMenuList(@PathVariable Long roleId){
        List<SysMenu> sysMenuList = sysMenuService.getRoleMenuList(roleId);
        return Result.ok(sysMenuList);
    }

    @ApiOperation("给角色分配权限,保存数据")
    @PutMapping("/assignMenu")
    public Result assignMenu(@RequestBody AssignMenuVo vo){
        sysMenuService.assignMenu(vo);
        return Result.ok();
    }

}
